const AuthService = require('../services/authService');
const { formatResponse } = require('../utils/transform');
const CaptchaService = require('../services/captchaService');

class AuthController {
  constructor() {
    this.authService = new AuthService();
    this.captchaService = new CaptchaService();
  }

  // 获取图形验证码
  async getCaptcha(ctx) {
    try {
      const data = this.captchaService.generateCaptcha();
      ctx.status = 200;
      ctx.body = formatResponse(200, '获取成功', data);
    } catch (error) {
      console.error('获取验证码错误:', error);
      ctx.status = 500;
      ctx.body = formatResponse(500, '获取失败', null, { error: error.message });
    }
  }

  // 登录控制器
  async login(ctx) {
    try {
      const body = ctx.request.body;
      const { username, password, captcha_id, captcha_code } = body;

      // 基本参数校验
      if (!captcha_id || !captcha_code) {
        ctx.status = 400;
        ctx.body = formatResponse(400, '验证码不能为空', null);
        return;
      }
      const ok = this.captchaService.verifyCaptcha(captcha_id, captcha_code);
      if (!ok) {
        ctx.status = 400;
        ctx.body = formatResponse(400, '验证码错误或已过期', null);
        return;
      }

      const result = await this.authService.login(username, password);

      if (result.success) {
        ctx.status = 200;
        ctx.body = formatResponse(200, result.message, result.data);
      } else {
        ctx.status = result.status || 500;
        ctx.body = formatResponse(
          result.status || 500,
          result.message,
          null,
          { error: result.error }
        );
      }
    } catch (error) {
      console.error('登录控制器错误:', error);
      ctx.status = 500;
      ctx.body = formatResponse(500, '登录失败，请稍后重试', null, { error: error.message });
    }
  }

  // 刷新令牌控制器
  async refreshToken(ctx) {
    try {
      const body = ctx.request.body;
      const { refresh_token } = body;

      const result = await this.authService.refreshToken(refresh_token);

      if (result.success) {
        ctx.status = 200;
        ctx.body = formatResponse(200, result.message, result.data);
      } else {
        ctx.status = result.status || 500;
        ctx.body = formatResponse(
          result.status || 500,
          result.message,
          null,
          { error: result.error }
        );
      }
    } catch (error) {
      console.error('刷新令牌控制器错误:', error);
      ctx.status = 500;
      ctx.body = formatResponse(500, '刷新令牌失败，请稍后重试', null, { error: error.message });
    }
  }

  // 登出控制器
  async logout(ctx) {
    try {
      const result = await this.authService.logout();

      if (result.success) {
        ctx.status = 200;
        ctx.body = formatResponse(200, result.message, null);
      } else {
        ctx.status = result.status || 500;
        ctx.body = formatResponse(
          result.status || 500,
          result.message,
          null,
          { error: result.error }
        );
      }
    } catch (error) {
      console.error('登出控制器错误:', error);
      ctx.status = 500;
      ctx.body = formatResponse(500, '登出失败，请稍后重试', null, { error: error.message });
    }
  }
}

module.exports = AuthController;